package gases.as3.librarys.compnents.ui.control
{
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import gases.as3.librarys.compnents.events.GListEvent;
	import gases.as3.librarys.compnents.ui.GUIComepments;
	import gases.as3.librarys.compnents.ui.control.list.ListCell;
	/**
	 *list
	 * 暂时提供了两个填充数据的。
	 * <p>1. dataProvider 填充label
	 * <p>2. iconField 填充icon
	 * <p>假如传值为普通的object。程序会自动识别里面的label 和 icon。进而自动填充
	 * <p> example 见TestingGList.as
	 * @author Administrator
	 */	
	public class GList extends GUIComepments
	{
		public function GList()
		{
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
		}
		
		override public function dispose():void
		{
			clearUI();
			_cellArr = null;
			_dataProvider = null;
			_iconField = null;
			super.dispose();
		}
		
		public function get iconField():*
		{
			return _iconField;
		}
		/**
		 *设置icon 
		 * @param value 数组
		 * 
		 */		
		public function set iconField(value:*):void 
		{
			_iconField = value;
			if(!value)
			{
				return;
			}
			if(value is Array)
			{
				decodeData(value as Array);
			}
		}
		
		public function get dataProvider():*
		{
			return _dataProvider;
		}
		/**
		 *设置label 
		 * @param value 数组
		 * 
		 */		
		public function set dataProvider(value:*):void 
		{
			_dataProvider = value;
			if(!value)
			{
				return;
			}
			if(value is Array)
			{
				decodeData(value as Array);
			}
		}
		
		/**
		 *由于没有写横着的滚动条，故暂时只改变高度
		 * @param width
		 * @param height
		 * 
		 */		
		public function setSize(w:Number = 0,h:Number = 0 ):void
		{
			
		}
		
		private function decodeData(arr:Array):void
		{
			if(arr.length == 0)
			{
				return;
			}
			if(!_cellArr)
			{
				_cellArr = new Array();
				for(var i:int = 0;i<arr.length;i++)
				{
					var cell:ListCell = new ListCell();
					var obj:Object;
					obj = arr[i];
					_cellArr.push(cell);
					if(obj["label"])
					{
						cell.updateCellLabel(obj["label"]);
					}
					if(obj["icon"])
					{
						cell.updateCellIcon(obj["icon"]);
					}
				}
				updateUI();
			}
			else
			{
				if(arr.length > _cellArr.length)
				{
					for(var k:int = 0;k < arr.length - _cellArr.length;k++)
					{
						_cellArr.push(new ListCell());
					}
				}
				for(var j:int = 0;j < arr.length;j++)
				{
					var listCell:ListCell = _cellArr[j] as ListCell;
					var object:Object;
					object = arr[j];
					if(object["label"])
					{
						listCell.updateCellLabel(object["label"]);
					}
					if(object["icon"])
					{
						listCell.updateCellIcon(object["icon"]);
					}
				}
				updateUI();
			}
		}
		
		private function updateUI():void
		{
			if(_cellArr.length == 0)
			{
				return;
			}
			clearUI();
			for(var i:int=0;i<_cellArr.length;i++)
			{
				var cell:ListCell = _cellArr[i] as ListCell;
				if(i != 0)
				{
					cell.y = (_cellArr[i -1] as ListCell).y +(_cellArr[i -1] as ListCell).height ;
				}
				cell.x = 0;
				this.addChild(cell);
				addListeners(cell);
			}
		}
		
		private function clearUI():void
		{
			for(var i:int=0;i<_cellArr.length;i++)
			{
				var cell:ListCell = _cellArr[i] as ListCell;
				if(this.contains(cell))
				{
					removeListeners(cell);
					this.removeChild(cell);
				}
			}
		}
		
		private function addListeners(ied:IEventDispatcher):void
		{
			ied.addEventListener(MouseEvent.MOUSE_OVER,onOverHander);
			ied.addEventListener(MouseEvent.MOUSE_OUT,onOutHander);
			ied.addEventListener(MouseEvent.CLICK,onClickHander);
		}
		
		private function removeListeners(ied:IEventDispatcher):void
		{
			ied.removeEventListener(MouseEvent.MOUSE_OVER,onOverHander);
			ied.removeEventListener(MouseEvent.MOUSE_OUT,onOutHander);
			ied.removeEventListener(MouseEvent.CLICK,onClickHander);
		}
		
		private function onOverHander(e:MouseEvent):void
		{
			var cell:ListCell = e.currentTarget as ListCell;
			cell.cellOver();
		}
		
		private function onOutHander(e:MouseEvent):void
		{
			var cell:ListCell = e.currentTarget as ListCell;
			cell.cellOut();
		}
		
		private function onClickHander(e:MouseEvent):void
		{
			var cell:ListCell = e.currentTarget as ListCell;
			var index:int;
			for(var i:int=0;i<_cellArr.length;i++)
			{
				var obj:ListCell = _cellArr[i] as ListCell;
				if(obj != cell)
				{
					obj.addEventListener(MouseEvent.MOUSE_OVER,onOverHander);
					obj.addEventListener(MouseEvent.MOUSE_OUT,onOutHander);
					obj.addEventListener(MouseEvent.CLICK,onClickHander);
					obj.cellOut();
				}
				else
				{
					cell.addEventListener(MouseEvent.MOUSE_OVER,onOverHander);
					cell.addEventListener(MouseEvent.MOUSE_OUT,onOutHander);
					cell.addEventListener(MouseEvent.CLICK,onClickHander);
					cell.cellClick();
					index = i;
				}
			}
			var object:Object = {bmd:cell.Bmd,txt:cell.Txt};
			this.dispatchEvent(new GListEvent(GListEvent.ITEM_CLICK,object,index));
		}
		/**
		 *icon的数组 
		 */		
		private var _iconField:*;
		/**
		 *label的数组 
		 */		
		private var _dataProvider:*;
		/**
		 *包含所有的cell的数组 
		 */		
		private var _cellArr:Array;
	}
}